package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasRepairGoods;
import com.kingint.mbg.bas.mapper.BasRepairGoodsMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasRepairGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 维修物品相关表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-22
 */
@Service
@Slf4j
public class BasRepairGoodsServiceImpl extends ServiceImpl<BasRepairGoodsMapper, BasRepairGoods> implements BasRepairGoodsService {

    @Resource
    private UmsAdminService adminService;
    @Resource
    private BasRepairGoodsMapper repairGoodsMapper;

    @Override
    public Object insertRepairGoodsInfo(BasRepairGoods basRepairGoods) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号未关联酒店id");
            return K.error();
        }
        //判断是否已经加了对应的分类
        Integer pid = basRepairGoods.getPid();
        if (pid == 0) {//大分类
            //判断是不是已经添加了
            Integer type = basRepairGoods.getType();
            QueryWrapper<BasRepairGoods> wrapper = new QueryWrapper<>();
            wrapper.eq("hotel_id", hotelId)
                    .eq("pid", 0)
                    .eq("type", type)
                    .eq("status", 0);
            Integer integer = repairGoodsMapper.selectCount(wrapper);
            if (integer != 0) {
                return K.error().data("已经添加当前分类的维修物品");
            }
        }
        basRepairGoods.setCreateTime(new Date());
        basRepairGoods.setHotelId(hotelId);
        int insert = repairGoodsMapper.insert(basRepairGoods);
        return K.ok().data(insert);
    }

    @Override
    public int updateRepairGoodsInfo(BasRepairGoods basRepairGoods) {
        int i = repairGoodsMapper.updateById(basRepairGoods);
        return i;
    }

    @Override
    public int deleteRepairGoodsInfo(Integer id) {
        //判断是不是一级分类的维修物品
        BasRepairGoods byId = repairGoodsMapper.selectById(id);
        Integer pid = byId.getPid();
        if (pid != null && pid == 0) {
            //将其逻辑删除，同时删除对应下的分类
            handleRepairInfo(byId.getId());
        }
        BasRepairGoods goods = new BasRepairGoods();
        goods.setId(id);
        goods.setStatus(1);
        int i = repairGoodsMapper.updateById(goods);
        return i;
    }

    /**
     * 处理对应的分类信息
     *
     * @param id
     */
    private void handleRepairInfo(Integer id) {
        QueryWrapper<BasRepairGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id)
                .eq("status", 0);
        List<BasRepairGoods> basRepairGoods = repairGoodsMapper.selectList(wrapper);
        if (basRepairGoods.size() != 0) {
            //全部置为1，逻辑删除
            for (BasRepairGoods info : basRepairGoods) {
                BasRepairGoods goods = new BasRepairGoods();
                goods.setId(info.getId());
                goods.setStatus(1);
                repairGoodsMapper.updateById(goods);
                handleRepairInfo(info.getId());
            }
        }
    }

    @Override
    public IPage<BasRepairGoods> getRepairGoodsList(BasRepairGoods basRepairGoods, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号未关联酒店id");
            return null;
        }
        IPage<BasRepairGoods> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BasRepairGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0)
                .eq("pid", 0);
        IPage<BasRepairGoods> goodsIPage = repairGoodsMapper.selectPage(page, wrapper);

        QueryWrapper<BasRepairGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasRepairGoods> goodsList = repairGoodsMapper.selectList(wrapper1);
        cancelRepairGoods(goodsIPage.getRecords(), goodsList);
        return goodsIPage;
    }

    private void cancelRepairGoods(List<BasRepairGoods> aClass, List<BasRepairGoods> allList) {
        if (!CollectionUtils.isEmpty(aClass)) {
            //为一级菜单设置子菜单
            for (BasRepairGoods info : aClass) {
                getChildList(info, allList);
            }
        }
    }

    private void getChildList(BasRepairGoods info, List<BasRepairGoods> aClass) {
        List<BasRepairGoods> serviceClass = info.getRepairGoodsList();
        if (CollectionUtils.isEmpty(serviceClass)) {
            serviceClass = new ArrayList<>();
        }
        for (BasRepairGoods ser : aClass) {
            if (ser.getPid() == info.getId()) {
                serviceClass.add(ser);
            }
        }
        //递归查询
        if (serviceClass.size() != 0) {
            for (BasRepairGoods child : serviceClass) {
                getChildList(child, aClass);
            }
        }
        info.setRepairGoodsList(serviceClass);
    }
}
